package com.hkws.hkws.API;

import com.hikvision.artemis.sdk.Client;
import com.hikvision.artemis.sdk.Request;
import com.hikvision.artemis.sdk.Response;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.hikvision.artemis.sdk.constant.Constants;
import com.hikvision.artemis.sdk.constant.ContentType;
import com.hikvision.artemis.sdk.constant.HttpHeader;
import com.hikvision.artemis.sdk.constant.MsgConstants;
import com.hikvision.artemis.sdk.enums.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.Map;

public class RootHttpUtilApi {

    private static final Logger logger = LoggerFactory.getLogger(RootHttpUtilApi.class);

    //调用网关成功的标志,标志位
    private final static String SUCC_PRE = "2";

    //调用网关重定向的标志,标志位
    private final static String REDIRECT_PRE = "3";

    /**
     * get请求
     *
     * @param path        配置的get请求的路径 是一个数组长度为1的Hashmap集合，只存一组数据，key为http的请求方式，value为host后面的path路径。
     * @param querys      map类型  get请求的url查询参数（url中的query参数,没有就是为空）形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型，该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息，该参数传空时的取默认值
     */

    public static String doGetUrl(Map<String, String> path, Map<String, String> querys, String accept, String contentType) {
        String httpSchema = (String) path.keySet().toArray()[0];
        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);
            String responseStr = null;
            try {
                Map<String, String> headers = new HashMap<String, String>();
                if (StringUtils.isNotBlank(accept)) {
                    headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
                } else {
                    headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
                }

                if (StringUtils.isNoneBlank(contentType)) {
                    headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
                } else {
                    headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
                }
                if (headers != null) {
                    headers.putAll(headers);
                }
                logger.info(path.get(httpSchema));
                Request request = new Request(Method.GET, httpSchema + ArtemisConfig.host,
                        path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
                request.setHeaders(headers);

                request.setQuerys(querys);

                Response response = Client.execute(request);
                responseStr = response.getBody();
            } catch (Exception e) {
                logger.error("the Artemis GET Request is failed[doGetArtemis]", e);
            }
            return responseStr;
        }
    }



